/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on a sample project from CommonsWare, LLC
 * From _The Busy Coder's Guide to Advanced Android Development_
 *		http://commonsware.com/AdvAndroid
 */

package edu.bsu.android.gtv.apps.bconnected.util;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

/**
 * Base class for a persistent list fragment, implementing the logic to show an activated highlight on a tapped 
 * item by marking the list row as being checked.
 * 
 * @author Kyle Parker, University Computing Services
 *
 */
public class PersistentListFragment extends ListFragment {
	static public final String STATE_CHECKED = "STATE_CHECKED";
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		l.setItemChecked(position, true);
	}
	
	@Override
	public void onSaveInstanceState(Bundle state) {
		state.putInt(STATE_CHECKED, getListView().getCheckedItemPosition());
	}
	
	protected void restoreState(Bundle state) {
		if (state!=null) {
			int position=state.getInt(STATE_CHECKED, -1);
			
			if (position>-1) {
				getListView().setItemChecked(position, true);
			}
		}
	}
	
	public void enablePersistentSelection() {
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
}
